package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperAddressDomainService;
import com.ymm.lulutong.entity.Address;
import com.ymm.lulutong.request.SaveAddressRequest;
import com.ymm.lulutong.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author zikang.yao
 * @Description TODO
 * @Date 2022/8/15 17:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/address")
public class ShipperAddressController {
    @Resource
    private ShipperAddressDomainService shipperAddressDomainService;

    /**
     * 根据货主Id获取地址簿
     * @param shipperId 货主Id
     * @return 地址簿
     */
    @PostMapping("/getAddressBook")
    public Result<List<Address>> getAddressBook(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperAddressDomainService.shipperListAddress(shipperId);
    }

    /**
     * 保存地址
     * @param saveAddressRequest 保存地址请求
     * @return 是否成功
     */
    @PostMapping("/save/newAddress")
    public Result<Boolean> saveNewAddress(SaveAddressRequest saveAddressRequest){
        if (Objects.isNull(saveAddressRequest)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperAddressDomainService.shipperSaveOftenAddress(saveAddressRequest);
    }

    @PostMapping("/delete/address")
    public Result<Boolean> deleteAddress(Long addressId){
        if (Objects.isNull(addressId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperAddressDomainService.shipperRemoveAddress(addressId);
    }
}
